package com.gmall.rabbitmq.consumer.service;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class RabbitMQServive_DeadLetter {

    //@RabbitHandler
    //@RabbitListener(queues = "Queue_Business_1",concurrency = "1")
    public void receive_SimpleMessage1(Channel channal, Message message,String msg) throws IOException {
        //ACK 默认自动发送ACK，MQ 收到ACK后就删除了该消息
        //ACK 手动模式：需要手动发送ACK
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        //1. 接收到消息
        try {
            System.out.println("------ Queue_Business_1 Start-------");
            System.out.println("MessageID:" + message.getMessageProperties().getMessageId());
            System.out.println("MessageBody:" + new String(message.getBody()));
            System.out.println("msg:" + msg);
            System.out.println("------ Queue_Business_1 End-------");
            //2.执行业务逻辑
            int i = 10/0;
            //3. 手动发送ACK

            channal.basicAck(deliveryTag,false);//手动确认，告诉MQ，我收到了，你可以删掉
        }catch (Exception e){
            System.out.println("------ basicNack -------" + deliveryTag);
            channal.basicNack(deliveryTag,false,false);
        }
    }

    @RabbitHandler
    @RabbitListener(queues = "Queue_DeadLetter_1")
    public void receive_SimpleMessage2(Channel channal, Message message,String msg) throws IOException {
        try {
            long deliveryTag = message.getMessageProperties().getDeliveryTag();
            System.out.println("------ Queue_DeadLetter_1 Start-------");
            System.out.println("MessageID:" + message.getMessageProperties().getMessageId());
            System.out.println("MessageBody:" + new String(message.getBody()));
            System.out.println("msg:" + msg);
            System.out.println("------ Queue_DeadLetter_1 End-------");

            channal.basicAck(deliveryTag, false);//手动确认，告诉MQ，我收到了，你可以删掉
        }catch (Exception e){

        }
    }

}
